package cn.xmoit.service;

import cn.xmoit.util.JGitUtils;
import org.apache.maven.shared.invoker.*;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;
import java.util.Collections;

/**
 * @author fangyy
 */
public class GitService {

	public static String localPath = "D:\\test";

	public static String remotePath = "https://gitee.com/yiyangduncuo/demo.git";

	public static String branch = "master";

	public static String username = "596179054@qq.com";

	public static String password = "6ff0d8edbbc9a968d1fffe32f88d6a45";

	public static void main1(String[] args) throws GitAPIException {

		/*
		 * 生成身份信息
		 *
		 * 注意：
		 *
		 * 1、github 下，无用户名，只需传 token，CredentialsProvider provider = new
		 * UsernamePasswordCredentialsProvider("","${access_token}");
		 *
		 * 2、gitee、bitbucket 下，需填写用户名 CredentialsProvider provider = new
		 * UsernamePasswordCredentialsProvider("${username}","${access_token}");
		 *
		 * 3、gitlab 下，用户名使用 "oauth2"，CredentialsProvider provider = new
		 * UsernamePasswordCredentialsProvider("oauth2","${access_token}");
		 */
		CredentialsProvider provider = new UsernamePasswordCredentialsProvider(username, password);
		Git git = Git.cloneRepository()
			// 设置git远端URL
			.setURI(remotePath)
			// 设置本地仓库位置
			.setDirectory(new File(localPath))
			// 设置身份验证
			.setCredentialsProvider(provider)
			// 设置是否克隆子仓库
			.setCloneSubmodules(true)
			// 设置克隆分支
			.setBranch(branch)
			// 启动命令
			.call();
		// 关闭源，以释放本地仓库锁
		git.close();
	}

	public static void main2(String[] args) throws GitAPIException {
		Git git = JGitUtils.open(localPath);
		git.add().addFilepattern(".").call();
		git.commit().setMessage("test commit 0003").call();
		/*
		 * 生成身份信息
		 *
		 * 注意：
		 *
		 * 1、github 下，无用户名，只需传 token，CredentialsProvider provider = new
		 * UsernamePasswordCredentialsProvider("","${access_token}");
		 *
		 * 2、gitee、bitbucket 下，需填写用户名 CredentialsProvider provider = new
		 * UsernamePasswordCredentialsProvider("${username}","${access_token}");
		 *
		 * 3、gitlab 下，用户名使用 "oauth2"，CredentialsProvider provider = new
		 * UsernamePasswordCredentialsProvider("oauth2","${access_token}");
		 */
		CredentialsProvider provider = new UsernamePasswordCredentialsProvider(username, password);
		git.push()
			// 设置推送的URL名称
			.setRemote("origin")
			// 设置需要推送的分支,如果远端没有则创建
			.setRefSpecs(new RefSpec(branch))
			// 身份验证
			.setCredentialsProvider(provider)
			.call();
		// 关闭源，以释放本地仓库锁
		git.close();
	}

	public static void main3(String[] args) throws GitAPIException {
		Git git = JGitUtils.open(localPath);
		git.rm().addFilepattern("New.txt").addFilepattern("New2.txt").addFilepattern("New3.txt").call();
		git.commit().setMessage("test commit 0004").call();
		/*
		 * 生成身份信息
		 *
		 * 注意：
		 *
		 * 1、github 下，无用户名，只需传 token，CredentialsProvider provider = new
		 * UsernamePasswordCredentialsProvider("","${access_token}");
		 *
		 * 2、gitee、bitbucket 下，需填写用户名 CredentialsProvider provider = new
		 * UsernamePasswordCredentialsProvider("${username}","${access_token}");
		 *
		 * 3、gitlab 下，用户名使用 "oauth2"，CredentialsProvider provider = new
		 * UsernamePasswordCredentialsProvider("oauth2","${access_token}");
		 */
		CredentialsProvider provider = new UsernamePasswordCredentialsProvider(username, password);
		git.push()
			// 设置推送的URL名称
			.setRemote("origin")
			// 设置需要推送的分支,如果远端没有则创建
			.setRefSpecs(new RefSpec(branch))
			// 身份验证
			.setCredentialsProvider(provider)
			.call();
		// 关闭源，以释放本地仓库锁
		git.close();
	}

	public static void main(String[] args) {
		InvocationRequest request = new DefaultInvocationRequest();
		request.setPomFile(new File("D:\\test\\pom.xml"));
		request.setGoals(Collections.singletonList("spring-javaformat:apply"));

		Invoker invoker = new DefaultInvoker();
		invoker.setMavenHome(new File("D:\\devtool\\apache-maven-3.5.0"));

		try {
			invoker.execute(request);
		}
		catch (MavenInvocationException e) {
			e.printStackTrace();
		}
	}

}
